package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageHelper;
import com.wangdao.wechatmall.bean.bo.admin.BaseIssueBO;
import com.wangdao.wechatmall.bean.bo.admin.IssueBO;
import com.wangdao.wechatmall.bean.pojo.Issue;
import com.wangdao.wechatmall.bean.pojo.IssueExample;
import com.wangdao.wechatmall.bean.vo.admin.IssueVO;
import com.wangdao.wechatmall.mapper.IssueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * IssueServiceImpl
 * created by feng on 7/8/21
 */

@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueVO getIssue(IssueBO issueBO) {
        // set PageHelper
        PageHelper.startPage(issueBO.getPage(), issueBO.getLimit());

        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();

        // 查询Question时传入的QuestionId
        if (issueBO.getQuestion() != null && !"".equals(issueBO.getQuestion())) {
            criteria.andQuestionLike("%" + issueBO.getQuestion() + "%");
        }

        // 未删除
        criteria.andDeletedEqualTo(0);

        // 实现排序
        issueExample.setOrderByClause(issueBO.getSort() + " " + issueBO.getOrder());

        List<IssueVO.ItemsDTO> issues = issueMapper.selectByExample(issueExample);

        int size = issues.size();
        IssueVO issueVO = new IssueVO();
        issueVO.setTotal(size);
        issueVO.setItems(issues);


        return issueVO;
    }

    @Override
    public Integer delete(Integer id) {
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdEqualTo(id);

        Issue issue = new Issue();
        Date date = new Date();

        // 设置更新时间
        issue.setUpdateTime(date);
        issue.setDeleted(1);

        int affectRow = issueMapper.updateByExampleSelective(issue, issueExample);
        return affectRow;
    }

    @Override
    public BaseIssueBO create(Issue issue) {
        // insert isssue
        issueMapper.insertSelective(issue);

        Issue insertedIssue = issueMapper.selectByPrimaryKey(issue.getId());

        BaseIssueBO baseIssueBO = new BaseIssueBO();

        // 回传
        baseIssueBO.setId(insertedIssue.getId());
        baseIssueBO.setAnswer(insertedIssue.getAnswer());
        baseIssueBO.setQuestion(insertedIssue.getQuestion());
        baseIssueBO.setAddTime(issue.getAddTime());
        baseIssueBO.setUpdateTime(issue.getUpdateTime());

        return baseIssueBO;
    }

    @Override
    public Integer update(Issue issue) {
        // issueExpamle
        IssueExample issueExample = new IssueExample();
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andIdEqualTo(issue.getId());

        // update updateTime
        issue.setUpdateTime(new Date());

        int affectedRow = issueMapper.updateByExample(issue, issueExample);

        return affectedRow;
    }
}